﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Psl.Applications;
using Psl.Remoting;

namespace Stl.Chat.Remoting.Plugin
{
    public partial class ConfigDialog : Form
    {
        public string DefaultServer { get; protected set; }
        public int DefaultPort { get; protected set; }
        public Channels.Protocol DefaultProtocol { get; protected set; }
        public Channels.Format DefaultFormat { get; protected set; }

        public string Server { get { return serverComboBoxIndex.Text; } }
        public int Port { get { return int.Parse(portComboBoxIndex.Text); } }
        public Channels.Protocol Protocol
        {
            get
            {
                if (httpRadioButton.Checked)
                    return Channels.Protocol.Http;
                else
                    return Channels.Protocol.Tcp;
            }
        }
        public Channels.Format Format { 
            get {
                if (soapRadioButton.Checked)
                    return Channels.Format.Soap;
                else
                    return Channels.Format.Binary;
            }
        }

        public ConfigDialog(String defaultServer,
                            int defaultPort,
                            Channels.Protocol defaultProtocol,
                            Channels.Format defaultFormat,
                            String server,
                            int port,
                            Channels.Protocol protocol,
                            Channels.Format format)
        {
            InitializeComponent();

            DefaultServer = defaultServer;
            DefaultPort = defaultPort;
            DefaultProtocol = defaultProtocol;
            DefaultFormat = defaultFormat;

            doUpdate(server, port, protocol, format);
        }
        
        protected void doUpdate(String server,
                            int port,
                            Channels.Protocol protocol,
                            Channels.Format format)
        {
            serverComboBoxIndex.Text = server;
            portComboBoxIndex.Text = port.ToString();
            if (protocol == Channels.Protocol.Http)
                httpRadioButton.Checked = true;
            else
                tcpRadioButton.Checked = true;
            if (format == Channels.Format.Soap)
                soapRadioButton.Checked = true;
            else
                binRadioButton.Checked = true;
        }

        private void yesButton_Click(object sender, EventArgs e)
        {
            /*
            if ("".Equals(Server))
                MessageBox.Show("Erreur : Le champs \"Server\" est vide.");
            */
            try
            {
                int port = Port;
                if (port < 0 || port > 65536)
                    throw new FormatException();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Erreur : Le port doit être un nombre entre 0 et 65536.");
            }
        }

        private void defaultButton_Click(object sender, EventArgs e)
        {
            serverComboBoxIndex.Text = DefaultServer;
            portComboBoxIndex.Text = DefaultPort.ToString();
            if (DefaultFormat == Channels.Format.Soap)
                soapRadioButton.Checked = true;
            else
                httpRadioButton.Checked = true;
            if (DefaultProtocol == Channels.Protocol.Http)
                httpRadioButton.Checked = true;
            else
                tcpRadioButton.Checked = true;
        }
    }
}
